﻿using System;
using System.Collections.ObjectModel;
using MySql.Data.MySqlClient;
using Reviewer.Data.Bases;
using Reviewer.Entities;
using Reviewer.Utilities.Data;
using System.Globalization;

namespace Reviewer.Data
{
    /// <summary>
    /// Game videdeos repository
    /// </summary>
    [CLSCompliant(false)]
    public class GameVideoRepository : RepositoryCrudAlternativeBase<GameVideo, Int32, Guid>
    {
        #region fields (private)
        private const string TABLE_NAME = "V2_SpilVideo";
        private static readonly CrudColumnInfo columnId = new CrudColumnInfo(0, "ID", MySqlDbType.Int32, true, false, false, true);
        private static readonly CrudColumnInfo columnTitle = new CrudColumnInfo(1, "Titel", MySqlDbType.VarChar, 255, false, false, true);
        private static readonly CrudColumnInfo columnLink = new CrudColumnInfo(2, "Link", MySqlDbType.VarChar, 255, false, false, true);
        private static readonly CrudColumnInfo columnText = new CrudColumnInfo(3, "Tekst", MySqlDbType.Text, false, false, true);
        private static readonly CrudColumnInfo columnGameId = new CrudColumnInfo(4, "SpilId", MySqlDbType.VarChar, 36, false, true, false);
        #endregion

        #region methods (protected)
        internal override CrudColumnInfo[] Columns
        {
            get
            {
                return new CrudColumnInfo[]{
                    columnId
                    , columnTitle
                    , columnLink
                    , columnText
                    , columnGameId
                };
            }
        }

        protected override string TableName
        {
            get { return GameVideoRepository.TABLE_NAME; }
        }

        internal override object ColumnToValue(CrudColumnInfo info, GameVideo item)
        {
            if (info.Equals(GameVideoRepository.columnId)) return item.Id;
            else if (info.Equals(GameVideoRepository.columnTitle)) return item.Title;
            else if (info.Equals(GameVideoRepository.columnLink)) return item.Link;
            else if (info.Equals(GameVideoRepository.columnText)) return item.Text;
            else if (info.Equals(GameVideoRepository.columnGameId)) return item.GameId;
            return null;
        }

        protected override GameVideo BuildItemFromReader(System.Data.IDataReader reader)
        {
            GameVideo item = new GameVideo();
            if (reader != null)
            {
                item.Id = DataReaderUtility.GetValue(reader, reader.GetOrdinal(GameVideoRepository.columnId.Name), 0);
                item.Title = DataReaderUtility.GetValue(reader, reader.GetOrdinal(GameVideoRepository.columnTitle.Name), String.Empty);
                item.Link = DataReaderUtility.GetValue(reader, reader.GetOrdinal(GameVideoRepository.columnLink.Name), String.Empty);
                item.Text = DataReaderUtility.GetValue(reader, reader.GetOrdinal(GameVideoRepository.columnText.Name), String.Empty);
                item.GameId = DataReaderUtility.GetValue(reader, reader.GetOrdinal(GameVideoRepository.columnGameId.Name), Guid.Empty);
            }
            return item;
        }
        #endregion

        public Collection<GameVideo> SelectLatest(int topCount)
        {
            using (MySqlCommand command = new MySqlCommand())
            {
                command.CommandText = this.SelectAllQuery() + " ORDER BY " + columnId.Name + " DESC LIMIT " + topCount.ToString(CultureInfo.InvariantCulture);
                return this.ExecuteReaderAndBuildList(command);
            }
        }
    }
}
